#include "runtimeinfo.h"
using namespace com_oocl_monitor::domain;

RunTimeInfo::RunTimeInfo(FtpProfileList &configs, int interval)
	: _interval(interval) {
	//pthread_rwlock_init(&_lock, NULL);
	InitializeSRWLock(&_lock);
	load(configs, interval);
}

RunTimeInfo::~RunTimeInfo() {
	_timeInfo.clear();
}
void RunTimeInfo::load(FtpProfileList &configs,int interval) {
	AcquireSRWLockExclusive(&_lock);
	for (auto it = configs.cbegin();it != configs.cend();++it) {
		for (auto itt = it->_profiles.cbegin();itt != it->_profiles.cend();++itt) {
			if (_timeInfo.find(itt->_profileName) == _timeInfo.cend()) {
				ptime endTime = second_clock::local_time() - minutes(interval);
				ptime startTime = second_clock::local_time() - hours(3*24);
				int runCount = 0;
				//init status to false
				Info info;
				info._startTime = startTime;
				info._endTime = endTime;
				info._runCount = runCount;
				info._isRunning = false;
				_timeInfo.insert(std::pair<string, Info>(itt->_profileName, info));

			}
		}
	}
	ReleaseSRWLockExclusive(&_lock);
}
ptime RunTimeInfo::getEndTime(const std::string &profileName) {
	AcquireSRWLockShared(&_lock);
	ptime t = _timeInfo.at(profileName)._endTime;
	ReleaseSRWLockShared(&_lock);
	return t;
}

void RunTimeInfo::setEndTime(const std::string &profileName, const ptime &endTime) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._endTime = endTime;
	ReleaseSRWLockExclusive(&_lock);
}

void RunTimeInfo::setStartTime(const std::string &profileName, const ptime &startTime) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._startTime = startTime;
	ReleaseSRWLockExclusive(&_lock);
}
ptime RunTimeInfo::getStartTime(const std::string &profileName) {
	AcquireSRWLockShared(&_lock);
	ptime p = _timeInfo.at(profileName)._startTime;
	ReleaseSRWLockShared(&_lock);
	return p;
}
void RunTimeInfo::setTime(const std::string &profileName) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._startTime = _timeInfo.at(profileName)._endTime;
	_timeInfo.at(profileName)._endTime += minutes(_interval);
	ReleaseSRWLockExclusive(&_lock);
}

int RunTimeInfo::getRuncount(const std::string &profileName) {
	AcquireSRWLockShared(&_lock);
	int t = _timeInfo.at(profileName)._runCount;
	ReleaseSRWLockShared(&_lock);
	return t;
}

void RunTimeInfo::incrRuncount(const std::string &profileName) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._runCount++;
	ReleaseSRWLockExclusive(&_lock);
}

void RunTimeInfo::resetRuncount(const std::string &profileName) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._runCount = 0;
	ReleaseSRWLockExclusive(&_lock);
}

void RunTimeInfo::setInterval(int interval) {
	AcquireSRWLockExclusive(&_lock);
	_interval = interval;
	ReleaseSRWLockExclusive(&_lock);
}

int RunTimeInfo::getInterval() {
	AcquireSRWLockShared(&_lock);
	int tmp = _interval;
	ReleaseSRWLockShared(&_lock);
	return tmp;
}
void RunTimeInfo::setStatus(const std::string &profileName, bool flag) {
	AcquireSRWLockExclusive(&_lock);
	_timeInfo.at(profileName)._isRunning = flag;
	ReleaseSRWLockExclusive(&_lock);
}
bool RunTimeInfo::getStatus(const std::string &profileName) {
	AcquireSRWLockShared(&_lock);
	bool status = _timeInfo.at(profileName)._isRunning;
	ReleaseSRWLockShared(&_lock);
	return status;
}
